<?php
// +----------------------------------------------------------------------
// | 邮箱设置
// +----------------------------------------------------------------------
// | Author: Eric <ieyangxiaoguo@126.com>
// +----------------------------------------------------------------------
// | Copyright ©2017 http://www.zzuyxg.top All rights reserved.
// +----------------------------------------------------------------------
// | 2017年9月28日
// +----------------------------------------------------------------------
namespace app\tdadmin\controller;

use app\common\controller\AdminBaseController;
use think\Db;

class SmsController extends AdminBaseController
{
    public function index()
    {
        if ($this->request->isPost()) {
            $post = array_map('trim', $this->request->param());
            
            set_option('sms_setting', $post);
            addLog("修改了短信配置",Db::getLastSql());

            $this->success("保存成功！");
        } else {
            $emailSetting = get_option('sms_setting');
            $this->assign($emailSetting);
            return $this->fetch();
        }
    }
    
    public function template()
    {
        if ($this->request->isPost()) {
            $templateKey  = $this->request->param('template_key');
            
            $data = $this->request->param();
            
            unset($data['template_key']);
            
            set_option($templateKey, $data);
            addLog("修改了短信模板",Db::getLastSql());


            $this->success("保存成功！");
        } else {

            $register =    $template = get_option('sms_template_register');
            $this->assign('register',$register);

            $reset =    $template = get_option('sms_template_reset_password');
            $this->assign('reset',$reset);

            $templates = Db::name('option')->where('option_name','like','sms_template_%')->all();
            foreach ($templates as $key => $value){
                $option = json_decode($value['option_value'],true);
                $templates[$key]['subject'] = $option['subject'];
                $templates[$key]['content'] = $option['content'];
                $templates[$key]['templateCode'] = $option['templateCode'];
            }
            $this->assign('templates',$templates);
            return $this->fetch();
        }
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $post = array_map('trim', $this->request->param());
            $templateKey  = $this->request->param('template_key');
            set_option('sms_template_'.$templateKey, ['subject'=>'','content'=>'', 'templateCode'=>'']);
            $this->success("保存成功！");
        }
    }
}

